package com.cyys.common.thirdInterface.bd;

import com.baidu.aip.ocr.AipOcr;
import com.cyys.common.ql.AjaxJson;
import com.cyys.common.thirdInterface.ThirdConfig;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

public class IDCardRecogizeUtil {

	private static final Map<String,String> SDK_ERROR_STATUS_DESC;
	static  
    {  
		SDK_ERROR_STATUS_DESC=  new HashMap<String,String>();  
		SDK_ERROR_STATUS_DESC.put("SDK100", "图片大小超限");  
		SDK_ERROR_STATUS_DESC.put("SDK101", "图片边长不符合要求");  
		SDK_ERROR_STATUS_DESC.put("SDK102", "读取图片文件错误");  
		SDK_ERROR_STATUS_DESC.put("SDK108", "连接超时或读取数据超时");  
		SDK_ERROR_STATUS_DESC.put("SDK109", "不支持的图片格式");  
    }
	
	private static final Map<String,String> IMAGE_STATUS_DESC;
	static  
    {  
		IMAGE_STATUS_DESC = new HashMap<String,String>();  
		IMAGE_STATUS_DESC.put("normal", "识别正常");  
		IMAGE_STATUS_DESC.put("reversed_side", "未摆正");  
		IMAGE_STATUS_DESC.put("non_idcard", "识别不到身份证");  
		IMAGE_STATUS_DESC.put("blurred", "信息模糊");  
		IMAGE_STATUS_DESC.put("over_exposure", "关键字段反光或过曝");  
		IMAGE_STATUS_DESC.put("unknown","识别异常");  
    }
	
	private static final Map<String,String> IDCARD_INFO_NAME;
	static  
    {  
		IDCARD_INFO_NAME = new HashMap<String,String>();  
		IDCARD_INFO_NAME.put("姓名", "name");  
		IDCARD_INFO_NAME.put("民族", "ethnicity");  
		IDCARD_INFO_NAME.put("住址", "address");  
		IDCARD_INFO_NAME.put("公民身份号码", "idCardNumber");  
		IDCARD_INFO_NAME.put("出生", "dateOfBirth");  
		IDCARD_INFO_NAME.put("性别","gender"); 
		IDCARD_INFO_NAME.put("失效日期", "expiryDate");  
		IDCARD_INFO_NAME.put("签发机关", "authority");  
		IDCARD_INFO_NAME.put("签发日期", "issueDate");  
    }
	
	public static AjaxJson IDCardRecogize(String frontImgUrl,  String backImgUrl) {
		AjaxJson j= new AjaxJson();
        AipOcr client = new AipOcr(ThirdConfig.BD_IDCARDRECOGIZE_APPID,
				ThirdConfig.BD_IDCARDRECOGIZE_API_KEY,
				ThirdConfig.BD_IDCARDRECOGIZE_SECRET_KEY);
        // 传入可选参数调用接口
        HashMap<String, String> options = new HashMap<String, String>();
        options.put("detect_direction", "true");
        options.put("detect_risk", "false");

        // 参数为本地图片路径
        JSONObject frontObj = client.idcard(frontImgUrl, "front", options);//身份证正面照路径
        String frontMsg= "识别异常";
        if(frontObj!= null) {
        	if(frontObj.has("image_status") && "normal".equals(frontObj.getString("image_status"))) {//image_status为normal时识别成功
        		frontMsg= null;
        	}else {//识别失败
        		if(frontObj.has("error_code")) {
            		String errorCode= frontObj.getString("error_code");
            		if(SDK_ERROR_STATUS_DESC.containsKey(errorCode)) {
            			frontMsg= SDK_ERROR_STATUS_DESC.get(errorCode);
            		}
            	}else {
            		frontMsg= IMAGE_STATUS_DESC.get(frontObj.getString("image_status"));
            	}
        	}
        }
        
        // 参数为本地图片路径
        JSONObject backObj = client.idcard(backImgUrl, "back", options);//身份证反面照路径
        String backMsg= "识别异常";
        if(backObj!= null) {
        	if(backObj.has("image_status") && "normal".equals(backObj.getString("image_status"))) {//image_status为normal时识别成功
        		backMsg= null;
        	}else {//识别失败
        		if(backObj.has("error_code")) {
            		String errorCode= backObj.getString("error_code");
            		if(SDK_ERROR_STATUS_DESC.containsKey(errorCode)) {
            			backMsg= SDK_ERROR_STATUS_DESC.get(errorCode);
            		}
            	}else {
            		backMsg= IMAGE_STATUS_DESC.get(backObj.getString("image_status"));
            	}
        	}
        }
        
        if(frontMsg== null && backMsg== null) { //身份证识别正常
        	j.setMsg("身份证识别正常");
        	JSONObject frontInfo = frontObj.getJSONObject("words_result");
        	for(String key: frontInfo.keySet()) {
        		if(IDCARD_INFO_NAME.containsKey(key)) {
        			JSONObject obj = frontInfo.getJSONObject(key);
        			j.put(IDCARD_INFO_NAME.get(key), obj.getString("words"));
        		}
        	}
        	
        	JSONObject backinfo = backObj.getJSONObject("words_result");
        	for(String key: backinfo.keySet()) {
        		if(IDCARD_INFO_NAME.containsKey(key)) {
        			JSONObject obj = backinfo.getJSONObject(key);
        			j.put(IDCARD_INFO_NAME.get(key), obj.getString("words"));
        		}
        	}
        }else {
        	String msg= "";
        	if(frontMsg!= null && backMsg!= null){
        		msg= "身份证正面照："+frontMsg+",身份证反面照："+backMsg;
        	}else if(frontMsg!= null){
        		msg= "身份证正面照："+frontMsg;
        	}else if(backMsg!= null){
        		msg= "身份证反面照："+backMsg;
        	}
        	j.setSuccess(false);
    		j.setErrorCode("701");
    		j.setMsg(msg);
        }
        return j;
   }
}
